import BaseGroup from '@/components/js/BaseGroup'
import { EventBus } from '@/store/index'
import { COMPONENT_TYPES, CONTROL_TYPES } from '@/common/const-wb'
import { getCompIdByParam } from '@/plugins/utils'

export default class ClickGetCard extends BaseGroup{
  constructor() {
    super();
    //子类覆盖
    this.cardMap = {};//{0:1,1:1};
    this.componentSpriteSheetParamsMap = {};
    this.itemIndex;
	}

  start(){
    this.refreshDraw = this.refreshDraw.bind(this);
    EventBus.$on('refreshDraw', this.refreshDraw);
    this.refreshDraw();
  }
  refreshDraw() {
    let relateCompId = getCompIdByParam(this.commonAttr.relateAttrCompId);
    if(!relateCompId){
      return
    }
    let relateCompData = this.componentMap[relateCompId];
    let cardMap = this.controls[relateCompData.id][CONTROL_TYPES.wb_judge_right_wrong].data.cardMap;
    if(!cardMap){
      return
    }
    _.forEach(this.components, (compData) => {
      if(compData.cid == COMPONENT_TYPES.wb_img){
        //值小于0变灰
        let commonAttr = {filter: { isGrayscale: 0, grayscale: 5 }};
        if(cardMap[this.itemIndex+1] <= 0){
          commonAttr.filter.isGrayscale = true;
        }else{
          commonAttr.filter.isGrayscale = false;
        }
        this.store.commit("updateComponentAttr", { 
          id: compData.id, 
          commonAttr,
        });

        if(compData.interactionData && compData.interactionData.spriteSheetObj){
          let spriteSheetObj = {};
          spriteSheetObj.currentStep = cardMap[this.itemIndex+1];
          this.store.commit("updateComponentAttr", { 
            id: compData.id, 
            interactionData: { spriteSheetObj },
          });
          EventBus.$emit('refreshImgSpriteSheet', { id: compData.id });
        }
        EventBus.$emit('refreshImgStyles', { id: compData.id });
      }else if(compData.cid == COMPONENT_TYPES.wb_text){
        let commonAttr = {text: cardMap[this.itemIndex+1]};
        this.store.commit("updateComponentAttr", { 
          id: compData.id, 
          commonAttr,
        });
      }
    })
    // this.store.commit("updateComponentAttr", { id: relateCompId, lists: relateCompList });
    // EventBus.$emit("itemClick", {id: this.item.id, index: 0, e: null});

    this.refreshDrawBtn();
  }

  refreshDrawBtn() {
    let compData = this.componentMap[id]
    let btnCompId = compData.interactionData.btnCompId
    if(btnCompId) {
      let btnCompData = this.componentMap[btnCompId]
      let count = 0
      _.forEach(cardMap, (value) => {
        if(value > 0){
          count++
        }
      });
      const commonAttr = {filter: { isGrayscale: false, grayscale: 5 }}
      let isGrey = true
      if(count < _.size(cardMap)){
        commonAttr.filter.isGrayscale = true
        isGrey = true
      }else{
        commonAttr.filter.isGrayscale = false
        isGrey = false
      }
      _.merge(btnCompData.commonAttr, commonAttr)
      if(btnCompData.cid == COMPONENT_TYPES.wb_img) {
        EventBus.$emit('refreshImgStyles', { id: btnCompId })
      } else {
        EventBus.$emit('btnStatus', { isGrey, compId: btnCompId })
      }
    }
  }

  destroy(){
    super.destroy();

    EventBus.$off('refreshDraw', this.refreshDraw);
  }
}